/*
 ============================================================================
 Name		: WSEngine.h
 Author	  :
 Version	 : 1.0
 Copyright   : Copyright Genera Software
 Description : CWSEngine declaration
 ============================================================================
 */

#ifndef WSENGINE_H
#define WSENGINE_H

// INCLUDES
#include <e32std.h>
#include <e32base.h>
#include "Interfaces.h"
#include "asynccommandhandler.h"
#include "ClientEngine.h"
#include "WSRequests.h"

class CWsObject;
class CProgressNote;

// CLASS DECLARATION

/**
 *  CWSEngine
 *
 */
class CWSEngine : public CBase,
    public MHTTPDataSupplier,
    public MHttpClientObserver,
    public MWsRequestor

    {
public:
    // Constructors and destructor

    /**
     * Destructor.
     */
    ~CWSEngine();

    /**
     * Two-phased constructor.
     */
    static CWSEngine* NewL();

    /**
     * Two-phased constructor.
     */
    static CWSEngine* NewLC();

public:
    // From MWsRequestor
    /**
     * Issues an asynchronous web services request.
     *
     * @param aRequestId - id of the request.
     * @param aReqObject - a request object that contains the request params
     * that will be converted to json.
     * @param aAdditionalParam - descriptor containing additional parameter if
     * meeded by the request or KNullDesC8.
     * @param aResponseHandler - observer that will handle this request.
     */
    void WSRequestL( const TInt aRequestID, MWsObject* aReqObject,
        const TDesC8& aAdditionalParam, const TInt aResId,
        MWsResponseHandler* aResponseHandler );
    /**
     * Cancels the current outstanding web services request.
     */
    void CancelWsRequest();
    /**
     * Checks to see if a ws request is outstanding.
     */
    TBool IsActive();

    /**
     * Method to ne implemented to handle when the wait dialog is
     * dismissed by the user.
     */
    void HandleProgressDismissedByUser();

private:
    // From MClientObserver

    void ClientEventL( const TInt aEvent );
    void ClientBodyReceivedL( const TDesC8& aBodyData );
    void ClientErrorL( const TInt aError );
    void CancelledL();

public:
    // MHTTPDataSupplier

    void ReleaseData();
    TBool GetNextDataPart( TPtrC8& aDataPart );
    TInt Reset();
    TInt OverallDataSize();

private:

    /**
     * Constructor for performing 1st stage construction
     */
    CWSEngine();

    /**
     * EPOC default constructor for performing 2nd stage construction
     */
    void ConstructL();
    /**
     * Starts a wait note dialog with the given text.
     *
     * @param aNote - the text to be displayed for the wait dialog.
     */
    void StartWaitNoteL( const TInt aResId );
    /**
     * Stops the current wait dialog.
     */
    void StopWaitNote();

    const TDesC8& GenerateUriL( const TWSRequest& aRequest,
        const TDesC8& aParam );

    void HandleWSResponseL();

private:
    // DATA
    TBool iRequestActive;
    TInt iCurrentWSRequest;
    RStringPool iStringPool;
    CHttpClient* iHttpClient;
    RBuf8 iRequestBuffer;
    RBuf8 iResponseBuffer;
    RBuf8 iAuthorizationString;
    CUri8* iUri;
    THttpRequestType iReqType;
    /*! @var iProgressNote Wait dialog.*/
    CProgressNote* iProgressNote;
    CJsonParser* iJsonParser;
    CJsonGenerator* iJsonGenerator;
    /*! @var iResponseHandler Response handler.*/
    MWsResponseHandler* iResponseHandler;
    };

#endif // WSENGINE_H
